package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.custom.BaseController;
import com.example.demo.entity.Insurance;
import com.example.demo.form.InsuranceForm;
import com.example.demo.service.InsuranceService;

@RestController
@RequestMapping(value = "/insurance")
public class InsuranceController extends BaseController<Insurance,Integer,InsuranceForm>{
	@Autowired
	private InsuranceService insuranceDAO;
	
	@RequestMapping(value="/findbyId")
	public Object byId(InsuranceForm form) {
		Insurance model = null;
		Integer id = form.getId();
		if(id!=null) {
			 model = insuranceDAO.findById(id);
		}	
		return new AjaxResult(model);	
	}
	//名称不重复
		@RequestMapping(value = "/onlyNameSave")
		@ResponseBody
		public Object save1(InsuranceForm form)  {
			Insurance model = new Insurance();
			Integer id = form.getId();
			if(id!=null) {
				model = insuranceDAO.findById(id);
			}
			List list = new ArrayList();
			for (Insurance Insurance1: insuranceDAO.findAll()) {
				String aaa = Insurance1.getName();
				list.add(aaa);
			}
			String f = form.getName();
			for (int i = 0; i < list.size(); i++) {
				String bbb = (String) list.get(i);
				if(bbb.equals(f)) {	
					return new AjaxResult( false,"保险公司名称重复");
				}
			}
			BeanUtils.copyProperties(form, model,"id");
			insuranceDAO.save(model);
			return new AjaxResult("数据保存成功");
		}
}
